package com.smc.plugin;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.lang.reflect.Array;
import java.lang.reflect.Field;

import dalvik.system.BaseDexClassLoader;
import dalvik.system.DexClassLoader;

public class Plugin {

    private static final String TAG = "Plugin";

    /**
     * 加载新的dex
     * 1. 找到宿主的dexelement
     * 2. 找到插件的dexelement
     * 3. 将插件的dexelement放到宿主的dexelement之前
     *
     * @param context
     * @param path
     * @return
     */
    public static boolean load(Context context, String path){

        File fp = new File(path);
        if (!fp.exists()){
            return false;
        }

        ClassLoader classLoader = context.getClassLoader();
        String name = classLoader.getClass().getName();
        Log.i(TAG, "name: " + name);

        Object pathListObject =getPathListInstance(classLoader, BaseDexClassLoader.class);
        Field dexElementsField = getDexElementsField(classLoader, BaseDexClassLoader.class);
        Object selfDexs = loadDex(classLoader, BaseDexClassLoader.class);
        Object pluginDexs = loadPluginDex(context,path);
        Object destDexs = megerDex(pluginDexs, selfDexs);
        try {
            dexElementsField.set(pathListObject, destDexs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    private static Field getDexElementsField(Object instance, Class clazz){
        Field pathListField = null;
        try {
            pathListField = clazz.getDeclaredField("pathList");
            pathListField.setAccessible(true);
            Object pathListValue = pathListField.get(instance);
            Class dexPathListClazz = pathListValue.getClass();
            Field dexElementsField = dexPathListClazz.getDeclaredField("dexElements");
            dexElementsField.setAccessible(true);
            return dexElementsField;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Object getPathListInstance(Object instance, Class clazz){
        Field pathListField = null;
        try {
            pathListField = clazz.getDeclaredField("pathList");
            pathListField.setAccessible(true);
            Object pathListValue = pathListField.get(instance);
            return pathListValue;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static Object loadDex(Object instance, Class clazz){
        Field pathListField = null;
        try {
            pathListField = clazz.getDeclaredField("pathList");
            pathListField.setAccessible(true);
            Object pathListValue = pathListField.get(instance);
            Class dexPathListClazz = pathListValue.getClass();
            Field dexElementsField = dexPathListClazz.getDeclaredField("dexElements");
            dexElementsField.setAccessible(true);
            return dexElementsField.get(pathListValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static Object loadPluginDex(Context context, String path){
        DexClassLoader dexClassLoader = new DexClassLoader(path, context.getCacheDir().getPath(), null, null);
        return loadDex(dexClassLoader, BaseDexClassLoader.class);
    }

    private static Object megerDex(Object dexs1, Object dexs2){
        int oldSize = Array.getLength(dexs1);
        int addSize = Array.getLength(dexs2);
        int length = oldSize + addSize;

        Class<?> clazz = Array.get(dexs1, 0).getClass();

        dexs1.getClass().getComponentType();

        Object obj = Array.newInstance(clazz, length);
        for(int i = 0;i < oldSize; i++) {
            Array.set(obj, i, Array.get(dexs1, i));
        }
        for(int i = 0; i < addSize; i++) {
            Array.set(obj, oldSize + i, Array.get(dexs2, i));
        }
        return obj;
    }
}

